//
//  NewsCollectionViewCell.m
//  kr_36
//
//  Created by lmt on 16/1/19.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsCollectionViewCell.h"
#import "AllTableViewCell.h"
#import "ActivityTableViewCell.h"
#import "LMTAFNetwork.h"
#import "AllModel.h"
#import "ActivityModel.h"
#import <UIImageView+WebCache.h>
#import "LMTTImeChuo.h"
#import "ActivityDetailController.h"
#import <MJRefresh.h>
@implementation NewsCollectionViewCell

- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}

// 重写set方法
-(void)setAsd:(NSString *)asd
{
    if (_asd != asd) {
        [_asd release];
        _asd = [asd copy];
    }
    
    // 数据
    [self dataHander];
    
    // 上拉加载
    [self loading];
    
    // 下拉刷新
    [self refresh];
}

#pragma mark - 下拉刷新
- (void)refresh
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        //延迟2秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
                       dispatch_get_main_queue(),^{
                           
                           // 刷新
                           [self dataHander];
                           //新版本是 MJ_header
                           [self.tableView.mj_header endRefreshing];
                       });
    }];
    self.tableView.mj_header = header;
    header.stateLabel.font = [UIFont systemFontOfSize:12];
}


- (void)loading
{
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        //延迟两秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            if ([_asd isEqualToString:@"activity"]) {
                // 活动
                
            } else {
                // 非活动
                [LMTAFNetwork getUrl:[NSString stringWithFormat:@"https://rong.36kr.com/api/mobi/news?pageSize=20&lastId=%@&columnId=all&pagingAction=down", _tempModel.feedId] body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
                    
                    NSMutableArray *arr = resposeObject[@"data"][@"data"];
                    NSMutableArray *tempArr = [AllModel modelHanderWithArray:arr];
                    
                    for (AllModel *m in tempArr) {
                        [self.dataArr addObject:m];
                    }
                    
                    [_tableView reloadData];
                    
                } failure:^(NSURLSessionDataTask *task, NSError *error) {
                    
                }];
            }
            
            [_tableView.mj_footer endRefreshing];
        });
    }];
    _tableView.mj_footer = footer;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc] init];
        [self.contentView addSubview:_tableView];
        [_tableView release];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    _tableView.frame = CGRectMake(0, 0, layoutAttributes.size.width, layoutAttributes.size.height);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[AllTableViewCell class] forCellReuseIdentifier:@"cell1"];
    [_tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell2"];
    
    
}

- (void)dataHander
{
    NSString *s = [NSString stringWithFormat:@"https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=%@&pagingAction=up", self.asd];
    [LMTAFNetwork getUrl:s body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        
        NSLog(@"~~%@", _asd);
        
        // 活动
        if ([_asd isEqualToString:@"activity"]) {
            NSMutableArray *arr = [NSMutableArray array];
            
            for (NSDictionary *dic in resposeObject[@"data"][@"data"]) {
                if (dic[@"activity"]) {
                    
                    [arr addObject:dic[@"activity"]];
                }
            }
            self.activityArr = [ActivityModel modelHanderWithArray:arr];
            [_tableView reloadData];
        } else {
            // 非活动
            NSMutableArray *arr = resposeObject[@"data"][@"data"];
            self.dataArr = [AllModel modelHanderWithArray:arr];
            [_tableView reloadData];
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_asd isEqualToString:@"activity"]) {
        return _activityArr.count;
    }
    _tempModel = _dataArr.lastObject;
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        

    // 非活动
    if (![_asd isEqualToString:@"activity"]) {
        AllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        AllModel *m = _dataArr[indexPath.row];
        cell.title.text = m.title;
        cell.columName.text = m.columnName;
        cell.name.text = m.name;
        [cell.tu sd_setImageWithURL:[NSURL URLWithString:m.featureImg]];
        [cell.touxiang sd_setImageWithURL:[NSURL URLWithString:m.avatar]];
        cell.time.text = [LMTTImeChuo compareCurrentTime:m.publishTime];
        return cell;
    } else {
        // 活动
        ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        ActivityModel *m = _activityArr[indexPath.row];
        cell.title.text = m.title;
        cell.time.text = m.activityTimeDesc;
        cell.zhuangtai.text = m.status;
        cell.address.text = m.address;
        return cell;
    }
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_asd isEqualToString:@"activity"]) {
        return 130;
    }
    return 165;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (![_asd isEqualToString:@"activity"]) {
        
        AllModel *m = _dataArr[indexPath.row];
        
        
        [self.delegate passValue:m.feedId];
        
    } else {
        
        ActivityModel *m = _activityArr[indexPath.row];
        
        [self.dataSource passTwoValue:m.url];
        
    }
    
}

@end
